package com.pshare.artemis.common.tool;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.pshare.artemis.common.bean.DataResult;
import com.pshare.artemis.common.service.UpYunService;

/**
 * 文件上传
 * 
 * @author 等待
 * @class UploadUtils.java
 * @time 2014-5-12 上午10:05:21
 * @QQ 2743569843
 */
public class PicUpYunUploadTool {
	// 运行前先设置好以下三个参数
		private static final String BUCKET_NAME = "bang-news";
		private static final String USER_NAME = "admin";
		private static final String USER_PWD = "admin111";

		/** 绑定的域名 */
		private static final String URL = "http://" + BUCKET_NAME
				+ ".b0.upaiyun.com";

		/** 根目录 */
		private static final String DIR_ROOT = "/";

	public static DataResult uploadImg(File File, String fileName) throws IOException {
		DataResult dr = new DataResult(); 
		// 要传到upyun后的文件路径
				String filePath = new SimpleDateFormat("yyyy-MM-dd").format(new Date()) +"/"+ new Date().getTime()  + "."+fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());

				UpYunService upyun    = new UpYunService(BUCKET_NAME, USER_NAME, USER_PWD);

				
				boolean result = upyun.writeFile(filePath, File, true);
				if(result){
					dr.setFilePath(URL+DIR_ROOT+filePath);
					dr.setResult("图片上传成功");
					dr.setSuccess(result);
				}else {
					dr.setResult("图片上传失败");
					 
				}
				
		return dr;
	}
	private static String isSuccess(boolean result) {
		return result ? " 成功" : " 失败";
	}
	
}
